lxyssl = require'lxyssl'
copas = require'copas2'
ssl = require'ssl'
--copas.WATCH_DOG_TIMEOUT=600
ssl.async(copas)
--ssl.debug=0
--ssl.async_timeout=0
--ssl.async_handshake=true
--copas.non_block_connect = true
t={}
completed={}
failed=0
d=5000
active = 0

local pause
function f(skt, me)
  active = active + 1
  local a,b,c = ssl.request('https://10.253.253.1:4433')
  if true and type(b) == "number" then 
    print(me, active, #a,b,c) 
  else
    print(me, active, #a,b,c) 
  end
  if type(b) ~= "number" or b >= 400 then 
    failed = failed + 1 
  end
  active = active - 1
end

function pulse()
  while d > 0 or active > 0 do
    copas.sleep(0.001,true)
    if d > 0 then 
      if active < 10000 then
        copas.addthread(f,d) 
        d = d - 1
      end
    else
      if active < 1 then 
        copas.loopexit() 
      end
    end
  end
end
copas.addthread(pulse)
print(copas.loop(0.001))
collectgarbage('collect')
collectgarbage('collect')
print(os.time(),collectgarbage('count'),failed) 
